<template>
  <div>
    <div id="slider" class="mui-slider">
      <div
        id="sliderSegmentedControl"
        class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted"
      >
        <div class="mui-scroll">
          <span 
          :class="['mui-control-item',item.id==0?'mui-active':'']"
          v-for="item in categoryList" 
          :key="item.id"
          @click="changeCategory(item.id)"
          >{{item.title}}</span>
        </div>
      </div>
    </div>

    <!-- 图片区域 -->
    <ul>
      <router-link tag="li" v-for="item in photoList" :key="item.id" :to="'/home/photoinfo/'+item.id">
        <!-- 注意：v-lazy要制定图片的地址 -->
        <img v-lazy="item.img_url">
        <div class="info">
          <h1 class="info_title">{{item.title}}</h1>
          <div class=" info_content">{{item.zhaiyao}}</div>
        </div>
      </router-link>
    </ul>
  </div>
</template>

<script>
import mui from "../../lib/mui/js/mui.js";

export default {
  data() {
    return {
        categoryList:[],
        photoList:[]
    };
  },
  created(){
      this.getPhotoCategory(),
      this.getPhotoByCategory(0)
  },
  methods:{
     async getPhotoCategory(){
        //获取图片地分类数据
       let {data}=await this.$http.get('/api/getimgcategory');
       if(data.status===0){
         data.message.unshift({title:"全部",id:0})
         this.categoryList=data.message;
       } 
       console.log(this.categoryList);
      },
      async getPhotoByCategory(id){
        let {data}=await this.$http.get('/api/getimages/'+id);
        if(data.status==0) return this.photoList=data.message;
      },
      changeCategory(id){
        this.getPhotoByCategory(id)
      }
  },
  mounted() {
    //当此钩子函数执行地时候，我们才可以进行控件和插件地初始化工作
    mui(".mui-scroll-wrapper").scroll({
      deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
    });
  }
};
</script>

<style lang="less" scoped>
#slider {
    touch-action: pan-x;
}
img[lazy=loading] {
  width: 40px;
  height: 300px;
  margin: auto;
}
li {
  position: relative;
  list-style: none;
  background-color: #ccc;
  margin-bottom: 10px;
  box-shadow: 0 2px 7px gray;
}
ul {
  margin: 0;
  padding: 0;
  text-align: center;
  padding: 10px;
}
ul li img {
  vertical-align: middle;
  width: 100%;
}
.info{
  position: absolute;
  bottom: 0;
  left: 0;
  color: #fff;
  background-color: rgba(0,0,0,.5);
  text-align: left;
  text-indent: 2em;
  max-height: 86px;
  .info_title{
    font-size: 14px;
  }
  .info_content{
    font-size: 13px;
  }
}
</style>